package com.hotelManager.www.service;

import com.hotelManager.www.dao.RoomDao;
import com.hotelManager.www.dao.RoomTypeDao;
import com.hotelManager.www.po.Room;
import com.hotelManager.www.po.RoomType;
import com.hotelManager.www.serviceInterface.RoomTypeServiceInterface;
import com.hotelManager.www.util.JDBCUtils;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class RoomTypeService implements RoomTypeServiceInterface {
    private final RoomTypeDao roomTypeDao = new RoomTypeDao();
    private final RoomDao roomDao = new RoomDao();

    @Override
    public Connection getConnection() throws SQLException {
        try {
            return JDBCUtils.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean create(RoomType roomType) {
        try (Connection conn = getConnection()) {
            return roomTypeDao.create(conn, roomType);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(RoomType roomType) {
        try (Connection conn = getConnection()) {
            return roomTypeDao.update(conn, roomType);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer id) {
        try (Connection conn = getConnection()) {
            return roomTypeDao.delete(conn, id);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public RoomType findById(Integer id) {
        return null;
    }




    @Override
    public List<RoomType> getRoomTypesByShopId(int shopId) {
        try (Connection conn = getConnection()) {
            System.out.println("查询店铺ID为" + shopId + "的房型"); // 添加调试日志
            List<RoomType> roomTypes = roomTypeDao.findByShopId(conn, shopId);
            if (roomTypes != null) {
                System.out.println("找到" + roomTypes.size() + "个房型"); // 添加调试日志
                // 直接使用RoomType表中的stock字段，不需要额外计算
                for (RoomType roomType : roomTypes) {
                    System.out.println("房型ID " + roomType.getId() + " 库存为: " + roomType.getStock());
                }
            }
            return roomTypes;
        } catch (SQLException e) {
            System.out.println("获取房型时发生SQL异常: " + e.getMessage()); // 添加调试日志
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            System.out.println("获取房型时发生其他异常: " + e.getMessage()); // 添加调试日志
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean createRoomTypeWithRooms(RoomType roomType, int stock) {
        Connection conn = null;
        try {
            conn = getConnection();
            conn.setAutoCommit(false);

            // 1. 创建房型
            if (!roomTypeDao.create(conn, roomType)) {
                conn.rollback();
                return false;
            }

            // 2. 生成对应数量的房间
            for (int i = 0; i < stock; i++) {
                Room room = new Room();
                room.setRoomTypeId(roomType.getId());
                room.setRoomCode(roomDao.generateUniqueRoomCode(conn, roomType.getId()));
                room.setStatus("available");

                if (!roomDao.create(conn, room)) {
                    conn.rollback();
                    return false;
                }
            }

            conn.commit();
            return true;
        } catch (SQLException e) {
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
            return false;
        } finally {
            if (conn != null) {
                try {
                    conn.setAutoCommit(true);
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public boolean isValidPrice(BigDecimal price) {
        return price != null && price.compareTo(BigDecimal.ZERO) >= 0;
    }

    public boolean updateRoomTypeWithRooms(RoomType roomType, int stock) {
        try (Connection conn = JDBCUtils.getConnection()) {
            conn.setAutoCommit(false);
            System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 开始更新房型，ID: " + roomType.getId());

            // 更新房型信息
            boolean roomTypeUpdated = roomTypeDao.update(conn, roomType);
            if (!roomTypeUpdated) {
                System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 房型信息更新失败");
                conn.rollback();
                return false;
            }
            System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 房型信息更新成功");

            // 删除旧的房间数据
            boolean roomsDeleted = roomDao.deleteByRoomTypeId(conn, roomType.getId());
            if (!roomsDeleted) {
                System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 删除旧房间数据失败");
                conn.rollback();
                return false;
            }
            System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 删除旧房间数据成功");

            // 创建新的房间数据
            System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 开始创建新房间，数量: " + stock);
            for (int i = 0; i < stock; i++) {
                Room room = new Room();
                room.setRoomTypeId(roomType.getId());
                room.setStatus("available");
                String roomCode = roomDao.generateUniqueRoomCode(conn, roomType.getId());
                System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 生成房号: " + roomCode);
                room.setRoomCode(roomCode);
                boolean roomCreated = roomDao.create(conn, room);
                if (!roomCreated) {
                    System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 创建新房间失败");
                    conn.rollback();
                    return false;
                }
            }
            System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 所有新房间创建成功");

            conn.commit();
            System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 事务提交成功");
            return true;
        } catch (Exception e) {
            System.out.println("[RoomTypeService.updateRoomTypeWithRooms] 发生异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteRoomTypeWithRooms(int roomTypeId) {
        try (Connection conn = JDBCUtils.getConnection()) {
            conn.setAutoCommit(false);

            // 删除房间数据
            boolean roomsDeleted = roomDao.deleteByRoomTypeId(conn,roomTypeId);
            if (!roomsDeleted) {
                conn.rollback();
                return false;
            }

            // 删除房型数据
            boolean roomTypeDeleted = roomTypeDao.delete(conn, roomTypeId);
            if (!roomTypeDeleted) {
                conn.rollback();
                return false;
            }

            conn.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
